package com.atguigu.serviceedu.service.impl;

import com.atguigu.serviceedu.client.VodClient;
import com.atguigu.serviceedu.entity.EduChapter;
import com.atguigu.serviceedu.entity.EduVideo;
import com.atguigu.serviceedu.entity.chaptervo.ChapterInfoVo;
import com.atguigu.serviceedu.entity.chaptervo.VideoVo;
import com.atguigu.commonutils.exception.GuliException;
import com.atguigu.serviceedu.mapper.EduChapterMapper;
import com.atguigu.serviceedu.service.EduChapterService;
import com.atguigu.serviceedu.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author xuan
 * @since 2020-04-13
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    EduVideoService eduVideoService;
    @Autowired
    VodClient vodClient;

    @Override
    public boolean editChapter(ChapterInfoVo chapterVo) {
        EduChapter chapter = new EduChapter();
        String chapterId = chapterVo.getId();
        BeanUtils.copyProperties(chapterVo,chapter);
        int result = 0;
        if(StringUtils.isEmpty(chapterId)){
            result = baseMapper.insert(chapter);
        }else{
            result = baseMapper.updateById(chapter);
        }
        if(result==0){
            throw new GuliException(2001,"新增或更新失败");
        }
        return result>0;
    }

    @Override
    public List<ChapterInfoVo> getChapterList(String courseId) {
        QueryWrapper<EduChapter> chapterWrapper = new QueryWrapper<>();
        QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<>();
        List<ChapterInfoVo> chapterInfoVoList = new ArrayList<>();
        chapterWrapper.eq("course_id",courseId);
        videoWrapper.eq("course_id",courseId);
        List<EduChapter> chapterList = baseMapper.selectList(chapterWrapper);
        List<EduVideo> videoList = eduVideoService.list(videoWrapper);

        for (EduChapter chapter : chapterList) {
            ChapterInfoVo chapterInfoVo = new ChapterInfoVo();
            BeanUtils.copyProperties(chapter,chapterInfoVo);
            List<VideoVo> videoVoList = new ArrayList<>();
            for (EduVideo video : videoList) {
                if(video.getChapterId().equals(chapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video,videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterInfoVo.setChildren(videoVoList);
            chapterInfoVoList.add(chapterInfoVo);
        }

        return chapterInfoVoList;
    }

    @Override
    public boolean deleteChapter(String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(wrapper);
        if(count > 0){
            throw new GuliException(20001,"该章节下有小节,不能删除");
        }
        return baseMapper.deleteById(chapterId) > 0;
    }
}
